/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.asset.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.admin.asset.model.entity.PartRecordsDO;
import top.continew.admin.asset.model.query.PartRecordsQuery;
import top.continew.admin.asset.model.resp.PartRecordsDetailResp;
import top.continew.admin.asset.model.resp.PartRecordsResp;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import top.continew.admin.asset.mapper.PartRecordsMapper;
import top.continew.admin.asset.model.entity.PartRecordsDO;
import top.continew.admin.asset.model.query.PartRecordsQuery;
import top.continew.admin.asset.model.req.PartRecordsReq;
import top.continew.admin.asset.model.resp.PartRecordsDetailResp;
import top.continew.admin.asset.model.resp.PartRecordsResp;
import top.continew.admin.asset.service.PartRecordsService;

/**
 * 配件业务实现
 *
 * @author CharlieYan
 * @since 2025/07/03 10:52
 */
@Service
@RequiredArgsConstructor
public class PartRecordsServiceImpl extends BaseServiceImpl<PartRecordsMapper, PartRecordsDO, PartRecordsResp, PartRecordsDetailResp, PartRecordsQuery, PartRecordsReq> implements PartRecordsService {

    @Override
    public PageResp<PartRecordsResp> page(PartRecordsQuery query, PageQuery pageQuery) {
        QueryWrapper<PartRecordsDO> queryWrapper = this.buildQueryWrapper(query);
        super.sort(queryWrapper, pageQuery);
        IPage<PartRecordsDetailResp> page = baseMapper.selectPartRecordsPage(new Page<>(pageQuery.getPage(), pageQuery
            .getSize()), queryWrapper);
        PageResp<PartRecordsResp> pageResp = PageResp.build(page, super.getListClass());
        pageResp.getList().forEach(this::fill);
        return pageResp;
    }

    @Override
    protected QueryWrapper<PartRecordsDO> buildQueryWrapper(PartRecordsQuery query) {
        String fuzzy = query.getFuzzy();

        return new QueryWrapper<PartRecordsDO>().and(StrUtil.isNotBlank(fuzzy), q -> q.like("tt.name", fuzzy)
            .or()
            .like("tt.code", fuzzy)
            .or()
            .like("tt.producer_name", fuzzy)
            .or()
            .like("tt.category_name", fuzzy)
            .or()
            .like("tt.sn", fuzzy));

    }
}